package com.itany.corejava.code10_io.file;

import java.io.File;

/**
 * @author MissChen
 * @date 2024年03月08日 14:05
 */
public class Test05_练习 {
    public static void main(String[] args) {
        try {
            remove("E:\\aaaa\\bbb\\vv","E:\\aaaa");
        } catch (FileNotExistException e) {
            System.out.println("文件不存在了");
        }
    }
    public static void remove(String path,String rootPath) throws FileNotExistException {
        File file=new File(path);
        System.out.println(file+"------------------------");
        if(!file.exists()){
            throw new FileNotExistException("该文件不存在");
        }
        if(file.isDirectory()){//判断是否是目录
            System.out.println(file.listFiles().length);
            if(file.listFiles().length==0){
                String filePath=file.getAbsolutePath();//E:\aaaa\bbb
                String parentPath=file.getParentFile().getAbsolutePath();//E:\aaaa
                // System.out.println(filePath);
                // System.out.println(parentPath);
                file.delete();
                System.out.println("已经删除空目录:"+file.getAbsolutePath());
                System.out.println("-----");
                System.out.println(!filePath.equals(rootPath));
                if(!filePath.equals(rootPath)){//E:\aaaa\bbb   E:\aaaa
                    remove(parentPath,rootPath);//remove("E:\aaaa","E:\aaaa")
                }else{
                    return;
                }

            }else{
                File[] files=file.listFiles();
                for(File f:files){
                   remove(f.getAbsolutePath(),rootPath);//E:\aaaa\bbb   E:\aaaa
                }
            }
        }


    }
}
